package com.shsubway.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shsubway.R;
import com.shsubway.model.AnswerEntity;
import com.shsubway.utils.ClearEditText;

import java.util.List;

/**
 * Created by Hu on 2017/2/27.
 */

public class MakeQAdapter extends BaseQuickAdapter<AnswerEntity, BaseViewHolder> {
    int QES_TYPE;

    public MakeQAdapter(int layoutResId, List<AnswerEntity> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, AnswerEntity item) {
        Log.e("item====", item.toString() + "");
        Log.e("pos====", QES_TYPE + "");

        helper
                .setText(R.id.choose_id, "选项" + getA2Z(helper.getAdapterPosition() - 1))
                .addOnClickListener(R.id.choose_del)
                .setChecked(R.id.choose_isture, item.isTure());

        CheckBox radioButton = helper.getView(R.id.choose_isture);

        radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    item.setTure(isChecked);
                }
            }
        });
        ClearEditText clearEditText = helper.getView(R.id.choose_ans);
      if (QES_TYPE == 2) {
            if (helper.getAdapterPosition() == 1) {
                item.setAnscontent("正确");
            } else {
                item.setAnscontent("错误");
            }
            clearEditText.setEnabled(false);
        }else{
            clearEditText.setEnabled(true);
        }
        clearEditText.setTag(item);
        clearEditText.clearFocus();

        clearEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //获得Edittext所在position里面的Bean，并设置数据
                AnswerEntity answerEntity = (AnswerEntity) clearEditText.getTag();
                answerEntity.setAnscontent(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //大部分情况下，Adapter里面有if必须有else
        if (!TextUtils.isEmpty(item.getAnscontent())) {
            clearEditText.setText(item.getAnscontent());
        } else {
            clearEditText.setText("");
        }

    }

    private String getA2Z(int i) {
//        String[] str = new String[26];
        return String.valueOf((char) (65 + i));

    }


    public void setQesType(int position) {
        this.QES_TYPE = position;
    }
}
